package com.conceal.client.service.impl;

import com.conceal.client.dao.ConcealWxSessionMapper;
import com.conceal.client.dao.ConcealWxUserInfoMapper;
import com.conceal.client.bean.ConcealWxSession;
import com.conceal.client.service.ConcealWxSessionService;
import com.conceal.client.utils.SimpleResponse;
import com.conceal.client.utils.HttpsUtil;
import com.conceal.client.utils.SpringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.UUID;

@Service
public class ConcealWxSessionServiceImpl implements ConcealWxSessionService {
    @Resource
    private ConcealWxSessionMapper concealWxSessionMapper;

    @Resource
    private ConcealWxUserInfoMapper concealWxUserInfoMapper;

    @Override
    public String verify(String appID, String appSecret, String code, String nickName, String avatar,String country,Integer gender,String language,String city,String province) {
        String sid=null;
        //请求微信服务器
        String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s"
                + "&js_code=%s&grant_type=authorization_code", appID, appSecret, code);
        try {
            SimpleResponse res=HttpsUtil.httpsGet(url);
            String text = res.getText();
            Integer cod = res.getCode();
            Hashtable<String, String> rc=SpringUtil.jsonToFlatMap(text);
            String openId=rc.get("openid");
            String sessionKey=rc.get("session_key");
            String ip=SpringUtil.remoteAddr();
            sid =UUID.randomUUID().toString().replaceAll("-", "");
            if(openId!=null&&sessionKey!=null){
                ConcealWxSession wxSession = new ConcealWxSession();
                Map<String, Object> map = new HashMap<String, Object>();
                wxSession.setSid(sid);
                wxSession.setAvatar(avatar);
                wxSession.setNickName(nickName);
                wxSession.setUserCountry(country);
                wxSession.setUserProvince(province);
                wxSession.setUserCity(city);
                wxSession.setOpenid(openId);
                wxSession.setSessionKey(sessionKey);
                wxSession.setIp(ip);
                wxSession.setUserGender(gender.toString());
                wxSession.setUserLanguage(language);
                concealWxSessionMapper.insert(wxSession);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return sid;
    }

    /**
     * 验证sid存在并且不超时
     * @param sid
     * @param timeoutSecond
     * @return
     */
    public  boolean validateSession(String sid, int timeoutSecond){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sid", sid);
        map.put("timeoutSecond", timeoutSecond);
        int count = concealWxSessionMapper.validateSession(map);
        boolean available = false;
        if(count>0){//会话没有超时
            available = true;
        }
        return available;
    }

    /**
     * 获取用户id
     *
     * @param sid
     * @param wxa_session_timeout
     * @return
     */
    @Override
    public String getPid( String sid, int wxa_session_timeout) {
        String pid=null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sid", sid);
        map.put("timeoutSecond", wxa_session_timeout);
        String openId =  concealWxSessionMapper.getOpenId(map);
        if(openId!=null){
            map.put("openId",openId);
            pid = concealWxUserInfoMapper.getPid(map);
        }
        return pid;
    }


}
